{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dolly LLM \n",
    "\n",
    "An exploration of free to use 12B-parameter LLM made by [Databricks](https://databricks.com/) fine tuned ~15k record instructions [corpus](https://github.com/databrickslabs/dolly/tree/master/data).\n",
    "\n",
    "[Dolly](https://huggingface.co/databricks/dolly-v2-12b) is available on huggingface transformers and accelerate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: transformers in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (4.27.4)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.11.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (0.13.3)\n",
      "Requirement already satisfied: filelock in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (3.10.0)\n",
      "Requirement already satisfied: packaging>=20.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (23.0)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (2022.10.31)\n",
      "Requirement already satisfied: numpy>=1.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (1.23.5)\n",
      "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (0.13.2)\n",
      "Requirement already satisfied: tqdm>=4.27 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (4.65.0)\n",
      "Requirement already satisfied: requests in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (2.28.2)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers) (6.0)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.11.0->transformers) (4.5.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->transformers) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->transformers) (1.26.15)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->transformers) (3.1.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->transformers) (2022.12.7)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: accelerate in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (0.18.0)\n",
      "Requirement already satisfied: psutil in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from accelerate) (5.9.4)\n",
      "Requirement already satisfied: numpy>=1.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from accelerate) (1.23.5)\n",
      "Requirement already satisfied: pyyaml in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from accelerate) (6.0)\n",
      "Requirement already satisfied: torch>=1.4.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from accelerate) (2.0.0)\n",
      "Requirement already satisfied: packaging>=20.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from accelerate) (23.0)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu11==11.7.101 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.7.101)\n",
      "Requirement already satisfied: filelock in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (3.10.0)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu11==11.7.99 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.7.99)\n",
      "Requirement already satisfied: nvidia-cufft-cu11==10.9.0.58 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (10.9.0.58)\n",
      "Requirement already satisfied: nvidia-cusparse-cu11==11.7.4.91 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.7.4.91)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.7.99 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.7.99)\n",
      "Requirement already satisfied: typing-extensions in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (4.5.0)\n",
      "Requirement already satisfied: sympy in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (1.11.1)\n",
      "Requirement already satisfied: networkx in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (3.0)\n",
      "Requirement already satisfied: nvidia-cusolver-cu11==11.4.0.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.4.0.1)\n",
      "Requirement already satisfied: nvidia-nvtx-cu11==11.7.91 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.7.91)\n",
      "Requirement already satisfied: triton==2.0.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (2.0.0)\n",
      "Requirement already satisfied: jinja2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (3.1.2)\n",
      "Requirement already satisfied: nvidia-cublas-cu11==11.10.3.66 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (11.10.3.66)\n",
      "Requirement already satisfied: nvidia-curand-cu11==10.2.10.91 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (10.2.10.91)\n",
      "Requirement already satisfied: nvidia-cudnn-cu11==8.5.0.96 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (8.5.0.96)\n",
      "Requirement already satisfied: nvidia-nccl-cu11==2.14.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.4.0->accelerate) (2.14.3)\n",
      "Requirement already satisfied: setuptools in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.4.0->accelerate) (67.6.0)\n",
      "Requirement already satisfied: wheel in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.4.0->accelerate) (0.40.0)\n",
      "Requirement already satisfied: cmake in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from triton==2.0.0->torch>=1.4.0->accelerate) (3.26.0)\n",
      "Requirement already satisfied: lit in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from triton==2.0.0->torch>=1.4.0->accelerate) (15.0.7)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from jinja2->torch>=1.4.0->accelerate) (2.1.2)\n",
      "Requirement already satisfied: mpmath>=0.19 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sympy->torch>=1.4.0->accelerate) (1.3.0)\n"
     ]
    }
   ],
   "source": [
    "! pip install transformers --upgrade\n",
    "! pip install accelerate --upgrade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import textwrap\n",
    "import torch\n",
    "from transformers import pipeline"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the model\n",
    "\n",
    "This will download about 24GB of model weights.\n",
    "\n",
    "Use the fp16 model for fast inference and low memory usage. `bfloat16` is supported in A100 GPUs.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "instruct_pipeline = pipeline(model=\"databricks/dolly-v2-12b\", torch_dtype=torch.bfloat16,\\\n",
    "                             trust_remote_code=True, device_map=\"auto\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query the model\n",
    "\n",
    "Now let's ask questions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human:  What are the primary colors?\n",
      "AI: Primary colors are red, yellow, and blue. Together these are used in\n",
      "combination to create the rest of the color pallet. Red + yellow = orange.\n",
      "Yellow + blue = green. Red + blue = purple.\n",
      "Human:  Make a recipe of chicken satay.\n",
      "AI: Combine all ingredients except oil in a bowl and mix well.  Pour some oil in\n",
      "a pan and heat it up. Then shape the mixture into small logs.  Place the logs in\n",
      "the pan and let it cook for a couple of minutes until each side is well coated\n",
      "with oil.\n",
      "Human:  What are the most popular ecommerce sites in the Philippines\n",
      "AI: 1. Shopee 2. GoShop 3. Trellon\n",
      "Human:  Prepare a travel itinerary. I am travelling from London to Boracay\n",
      "Philippines on June 10-15.\n",
      "AI: Date: 06/10/2018  Destination: Boracay Philippines  Day 1  06/10/2018\n",
      "07:00 am  Arrive Boracay  09:00 am  Check-in to resort  12:00 pm  Chillaxing at\n",
      "Resort  Day 2  06/11/2018   07:00 am  Scuba Diving (With admission)  09:00 am\n",
      "Hale and greet the resort  12:00 pm  More chillaxing at resort  Day 3\n",
      "06/12/2018   07:00 am  Philippine Fishing  09:00 am  Departure from Boracay\n",
      "12:00 pm  Arrive back to London\n",
      "Human: : bye\n",
      "AI: Bye!\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    input_prompt = \"Human: \"\n",
    "    query = input(input_prompt)\n",
    "\n",
    "    if query.lower() == \"bye\":\n",
    "        text = f\"{input_prompt}: {query}\"\n",
    "        print(textwrap.fill(text, width=80))\n",
    "        print(\"AI: Bye!\")\n",
    "        break\n",
    "\n",
    "    text = f\"{input_prompt} {query}\"\n",
    "    print(textwrap.fill(text, width=80))\n",
    "    # print text within page width\n",
    "    response = instruct_pipeline(query)\n",
    "    text = f\"AI: {response}\"\n",
    "    print(textwrap.fill(text, width=80))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
